package com.zhj.sharding.config;

import org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * 应用自动装配
 */
@Configuration
public class ApplicationAutoConfiguration {


    @Bean
    public DataSource dataSource() throws URISyntaxException, SQLException, IOException {
        URL url = ClassLoader.getSystemResource("sharding/sharding-dev.yml");
        if (url == null) {
            throw new RuntimeException("配置文件为空");
        }
        File file = new File(url.getFile());
        return YamlShardingSphereDataSourceFactory.createDataSource(file);
    }

}
